 function IndexedDB(options, callback) {

     callback = callback || function () {};

     var openDB = function (callback) {
         callback = callback || function () {};
         var version = options.version || 1;
         var name = options.name || 'IndexedDB';
         var request = window.indexedDB.open(name, version);
         request.onerror = function (e) {
             callback(null, e);
             console.log(e.currentTarget.error.message);
         };
         request.onsuccess = function (e) {
             var db = e.target.result;
             callback(db);
         };
         request.onupgradeneeded = function (e) {
             var db = e.target.result;
             if (!db.objectStoreNames.contains(options.tabname)) {
                 var store = db.createObjectStore(options.tabname, {
                     keyPath: 'key'
                 });
                 store.createIndex('etagIndex', 'etag', {
                     unique: false
                 });
                 store.createIndex('lmsIndex', 'lms', {
                     unique: false
                 });
                 store.createIndex('timedbIndex', 'timedb', {
                     unique: true
                 });
                 store.createIndex('valueIndex', 'value', {
                     unique: true
                 });
             }
             callback(db);
         };
     }
     this.deleteDB = function (name) {
         indexedDB.deleteDatabase(name);
     }
     this.addData = function (storeName, value, callback) {
         callback = callback || function () {};
         openDB(function (db, error) {
             var transaction = db.transaction(storeName, 'readwrite');
             var store = transaction.objectStore(storeName);
             store.add(value);

             db.close()
             callback()
         })
     }
     this.getDataByKey = function (storeName, key, callback) {
         callback = callback || function () {};

         openDB(function (db, error) {
             if (error) {

             } else {
                 var transaction = db.transaction(storeName, 'readwrite');
                 var store = transaction.objectStore(storeName);
                 var request = store.get(key);
                 request.onsuccess = function (e) {
                     var student = e.target.result;

                     db.close()
                     callback(student);
                 };
             }
         })


     }

     this.updateDataByKey = function (storeName, key, value, callback) {
         callback = callback || function () {};

         openDB(function (db, error) {
             var transaction = db.transaction(storeName, 'readwrite');
             var store = transaction.objectStore(storeName);
             var request = store.get(key);
             request.onsuccess = function (e) {
                 var student = e.target.result;
                 for (var key in value) {
                     student[key] = value[key]
                     store.put(student);

                     db.close()
                     callback()
                 };
             }
         })
     }

     this.deleteDataByKey = function (storeName, key, callback) {
         callback = callback || function () {};

         openDB(function (db, error) {
             var transaction = db.transaction(storeName, 'readwrite');
             var store = transaction.objectStore(storeName);
             store.delete(key);

             db.close()
             callback()
         })
     }

     this.clearObjectStore = function (storeName, callback) {
         callback = callback || function () {};

         openDB(function (db, error) {
             var transaction = db.transaction(storeName, 'readwrite');
             var store = transaction.objectStore(storeName);
             store.clear();

             db.close()
             callback()
         })
     }

     function deleteObjectStore(storeName, callback) {
         callback = callback || function () {};

         openDB(function (db, error) {
             var transaction = db.transaction(storeName, 'versionchange');
             db.deleteObjectStore(storeName);

             db.close()
             callback()
         })
     }

     function fetchStoreByCursor(db, storeName) {
         var transaction = db.transaction(storeName);
         var store = transaction.objectStore(storeName);
         var request = store.openCursor();
         request.onsuccess = function (e) {
             var cursor = e.target.result;
             if (cursor) {
                 console.log(cursor.key);
                 var currentStudent = cursor.value;
                 console.log(currentStudent.name);
                 cursor.continue();
             }
         };
     }

     function getDataByIndex(db, storeName) {
         var transaction = db.transaction(storeName);
         var store = transaction.objectStore(storeName);
         var index = store.index("ageIndex");
         index.get(26).onsuccess = function (e) {
             var student = e.target.result;
             console.log(student.id);
         }
     }

     function getMultipleData(db, storeName) {
         var transaction = db.transaction(storeName);
         var store = transaction.objectStore(storeName);
         var index = store.index("nameIndex");
         var request = index.openCursor(null, IDBCursor.prev);
         request.onsuccess = function (e) {
             var cursor = e.target.result;
             if (cursor) {
                 var student = cursor.value;
                 console.log(student.name);
                 cursor.continue();
             }
         }
     }


     openDB(function (db) {
         if (db) {
             db.close()
             callback()
         }
     })
 }
 module.exports = IndexedDB;